package com.itheima.service;

import com.itheima.dao.StudentMapper;
import com.itheima.pojo.PageBean;
import com.itheima.pojo.Student;
import com.itheima.util.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.util.List;

public class StudentService {
    public PageBean<Student> selectByPage(int currentPage, int pageSize) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        StudentMapper mapper = sqlSession.getMapper(StudentMapper.class);

        // 创建用于封装返回结果的对象
        PageBean<Student> pb = new PageBean<Student>();
        // 设置当前页页码
        pb.setCurrentPage(currentPage);
        // 设置每页显示的条数
        pb.setPageSize(pageSize);
        // 查询总页数,并封装到PageBean中
        int totalCount = mapper.queryTotalCount();
        pb.setTotalCount(totalCount);
        // 查询当前页的数据信息,并封装到Pb中
        //计算起始索引
        int startIndex = (currentPage-1)*pageSize;
        List<Student> stuList = mapper.queryStudentByPage(startIndex,pageSize);
        pb.setList(stuList);
        // 关闭sessionSql
        sqlSession.close();
        return pb;
    }
}
